﻿function VesselMgmt() {
    this.VesselTabStrip = null;
    this.SearchGrid = null;
    this.NameCtrl = null;
    this.IMONumCtrl = null;
    this.CallSignCtrl = null;
    this.PhoneCtrl = null;
    this.FaxCtrl = null;
    this.EmailCtrl = null;
    this.HullCtrl = null;
    this.PortCtrl = null;
    this.FlagCtrl = null;
    this.ClassCtrl = null;
    this.DisableCtrl = null;
    this.FileUploadCtrl = null;
    this.DocsGrid = null;
    this.TemplateComboCtrl = null;
    this.CurrentSystemsTRVCtrl = null;
    this.JobsGrid = null;

    //The following are the controls to show data on selection of an item from systems tree
    this.VesselItemName = null;
    this.VesselItemDescription = null;
    this.VesselItemSpecifications = null;
    this.VesselItemType = null;
    this.VesselItemIsBizCritical = null;
    this.VesselItemIsEnvCritical = null;
    this.VesselItemIsSafetyCritical = null;
    this.VesselItemIsMandatory = null;

    this.CheckUserInput = function () {
        var roleName = this.NameCtrl.get_value();
        var roleDescription = this.NameCtrl.get_value();

        var regExp = new RegExp("^[a-zA-Z0-9_]{2,255}$");
        var regExp2 = new RegExp("^[a-zA-Z0-9_\"\$%&@\!/\n\\s,.:\(\)]{2,255}$");

        if (regExp.test(roleName)) {
            if (regExp2.test(roleDescription)) {
                return true;
            }
        }

        N_NotifyMessage('Invalid Data', 'Check if the role name or description has any of the follwing:<ul><li>Special Chanrectors (-#^*{}[]|-+=<>?\\)</li><li>Exceeds more than 255 charecors or less then 2 charectors.</li></ul>', 3);
        return false;
    }

    this.GetActiveTabIndex = function () {
        if (this.VesselTabStrip != null) {
            return this.VesselTabStrip.get_selectedTab().get_index();
        }
        else {
            throw 'Cannot load RadTabStrip.';
        }
    }

    this.SelectTabByName = function (pName) {
        if (this.VesselTabStrip != null) {
            return this.VesselTabStrip.findTabByText(pName).set_selected(true);
        }
        else {
            throw 'Cannot load vessel Tabs.';
        }
    }

    this.SelectTabByIndex = function (pTabIdx) {
        if (this.VesselTabStrip != null) {
            this.VesselTabStrip.get_tabs().getTab(pTabIdx).select();
        }
        else {
            throw 'Cannot load vessel Tabs.';
        }
    }

    this.PerformNewOperation = function () {
        LoadCurrentVesselData(null);
    }

    this.LoadCurrentVesselData = function (pVesselObject) {
        MASTER_StoreCurrentBusinessObject(pVesselObject);
        if (this.CheckCtrls()) {
            if (pVesselObject != null) {
                this.NameCtrl.set_value(pVesselObject.Name);
                this.IMONumCtrl.set_value(pVesselObject.ImoNumber);
                this.CallSignCtrl.set_value(pVesselObject.CallSign);
                this.PhoneCtrl.set_value(pVesselObject.HullNo);
                this.FaxCtrl.set_value(pVesselObject.PortOfRegistry);
                this.EmailCtrl.set_value(pVesselObject.Flag);
                this.HullCtrl.set_value(pVesselObject.InmarsatPhoneNo);
                this.PortCtrl.set_value(pVesselObject.Fax);
                this.FlagCtrl.set_value(pVesselObject.EMail);
                this.ClassCtrl.set_value(pVesselObject.ClassificationSociety);
                this.DisableCtrl.checked = pVesselObject.IsDisabled;

                if (pVesselObject.TemplateID != null) {
                    this.TemplateComboCtrl.findItemByValue(pVesselObject.TemplateID).select();
                    loadTemplateMachines(pVesselObject.TemplateID);
                }
                else {
                    this.TemplateComboCtrl.findItemByValue('0').select();
                }

                
            }
            else {
                this.NameCtrl.set_value('');
                this.IMONumCtrl.set_value('');
                this.CallSignCtrl.set_value('');
                this.PhoneCtrl.set_value('');
                this.FaxCtrl.set_value('');
                this.EmailCtrl.set_value('');
                this.HullCtrl.set_value('');
                this.PortCtrl.set_value('');
                this.FlagCtrl.set_value('');
                this.ClassCtrl.set_value('');
                this.DisableCtrl.checked = false;
                this.TemplateComboCtrl.findItemByValue('0').select();

            }
            this.SelectTabByIndex(1);
        }
    }

    this.PerformSaveOperation = function () {
        if (this.CheckCtrls()) {
            var currentVesselObject = MASTER_GetCurrentBusinessObject();
            if (currentVesselObject == null) {
                //New Object
                currentVesselObject = new Landscape.Vessel();
                currentVesselObject.ID = '00000000-0000-0000-0000-000000000000';
                currentVesselObject.CreatedBy = MASTER_GetCurrentUserCode();
                currentVesselObject.CreatedDate = new Date();
            }

            currentVesselObject.Name = this.NameCtrl.get_value().trim();
            currentVesselObject.ImoNumber = this.IMONumCtrl.get_value().trim();
            currentVesselObject.CallSign = this.CallSignCtrl.get_value().trim();
            currentVesselObject.HullNo = this.HullCtrl.get_value().trim();
            currentVesselObject.PortOfRegistry = this.PortCtrl.get_value().trim();
            currentVesselObject.Flag = this.FlagCtrl.get_value().trim();
            currentVesselObject.InmarsatPhoneNo = this.PhoneCtrl.get_value().trim();
            currentVesselObject.Fax = this.FaxCtrl.get_value().trim();
            currentVesselObject.EMail = this.EmailCtrl.get_value().trim();
            currentVesselObject.ClassificationSociety = this.ClassCtrl.get_value().trim();
            currentVesselObject.IsDisabled = this.DisableCtrl.checked
            currentVesselObject.IsDeleted = false;
            currentVesselObject.UpdatedBy = MASTER_GetCurrentUserCode();
            currentVesselObject.UpdatedDate = new Date();
            currentVesselObject.TemplateID = this.TemplateComboCtrl.get_value();

            if (currentVesselObject.TemplateID == "0")
                currentVesselObject.TemplateID = null;

            switch (this.GetActiveTabIndex()) {
                case 0:
                    {//Search Tab
                        break;
                    }

                case 1:
                    {//Details Tab
                        Landscape.AdministrativeActions.SaveVessel(currentVesselObject, function (result, e) {
                            e.ID = result;

                            MASTER_StoreCurrentBusinessObject(e);
                            N_NotifyMessage('Success !!!', 'The vessel details saved successfully.', 1);
                        }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); }, currentVesselObject);
                        break;
                    }
                case 2:
                    {
                        //Documents Tab
                        break;
                    }
                case 3:
                    {//Systems Template
                        Landscape.AdministrativeActions.SaveVessel(currentVesselObject, function (result, e) {
                            e.ID = result;
                            loadTemplateMachines(e.TemplateID);

                            MASTER_StoreCurrentBusinessObject(e);
                            N_NotifyMessage('Success !!!', 'The vessel details saved successfully.', 1);
                        }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); }, currentVesselObject);
                        break;
                    }
            }
        }
    }

    this.PerformListOperation = function () {
        var rgSearchResults = this.SearchGrid;

        switch (this.GetActiveTabIndex()) {
            case 0:
                {//Search Tab
                    rgSearchResults.clearSelectedItems();

                    Landscape.AdministrativeActions.GetVesselListByName('', rgSearchResults.get_masterTableView().get_currentPageIndex(), rgSearchResults.get_masterTableView().get_pageSize(),
                        function (result, e) {
                            e.get_masterTableView().set_dataSource(result);
                            e.get_masterTableView().dataBind();
                        }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); }, this.SearchGrid);

                    break;
                }
        }
    }

    this.ReleaseJob = function (jobID) {
        Landscape.MaintenanceActions.ReleaseJob(jobID, MASTER_GetCurrentUserCode(), function (result, e) {
            e.LoadJobsByVesselItem(e.GetSelectedVesselSystemID(), e.JobsGrid.get_masterTableView().get_currentPageIndex(), e.JobsGrid.get_masterTableView().get_pageSize());
            N_NotifyMessage('Released !!!', 'Selected job was released. If synchronizer is running then this job will bese sent to the vessel.', 1);
        }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); }, this);
    }

    this.DeleteJob = function (jobID) {
        Landscape.MaintenanceActions.DeleteJob(jobID, MASTER_GetCurrentUserCode(), function (result, e) {
            e.LoadJobsByVesselItem(e.GetSelectedVesselSystemID(), e.JobsGrid.get_masterTableView().get_currentPageIndex(), e.JobsGrid.get_masterTableView().get_pageSize());
            N_NotifyMessage('Deleted !!!', 'Selected job was deleted.', 1);
        }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); }, this);
    }

    this.RemoveVesselItem = function (vesselItemCode) {        
        Landscape.MaintenanceActions.RemoveVesselItem(vesselItemCode, function (result, e) {
            N_NotifyMessage('Removed !!!', 'Selected vessel item was removed.', 1);
            loadTemplateMachines(MASTER_GetCurrentBusinessObject().TemplateID);
        }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); }, this);
    }

    this.AssignItemToVessel = function (itemNode) {
        var vesselCode = MASTER_GetCurrentBusinessObject().ID;
        var parentVslItem = (itemNode.get_parent() != null ? itemNode.get_parent().get_attributes().getAttribute('VsselItemCode') : '00000000-0000-0000-0000-000000000000'); ;
        var itemCode = itemNode.get_value();
        var userCode = MASTER_GetCurrentUserCode();

        Landscape.MaintenanceActions.AddItem(itemCode, vesselCode, parentVslItem, userCode, function (result, e) {
            N_NotifyMessage('Assigned !!!', 'Selected item was assigned to the vessel.', 1);
            loadTemplateMachines(MASTER_GetCurrentBusinessObject().TemplateID);
        }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); });
    }

    this.CheckCtrls = function () {
        var hasError = false;
        var errorMsg = '';

        if (this.VesselTabStrip == null) {
            hasError = true;
            errorMsg += 'Unable to load vessel tabs.';
        }

        if (this.SearchGrid == null) {
            hasError = true;
            errorMsg += 'Unable to load search grid.';
        }

        if (this.NameCtrl == null) {
            hasError = true;
            errorMsg += 'Unable to load name.';
        }

        if (this.IMONumCtrl == null) {
            hasError = true;
            errorMsg += 'Unable to load IMO Number.';
        }

        if (this.CallSignCtrl == null) {
            hasError = true;
            errorMsg += 'Unable to load call sign.';
        }

        if (this.PhoneCtrl == null) {
            hasError = true;
            errorMsg += 'Unable to load phone number.';
        }

        if (this.FaxCtrl == null) {
            hasError = true;
            errorMsg += 'Unable to load FAX number.';
        }

        if (this.HullCtrl == null) {
            hasError = true;
            errorMsg += 'Unable to load hull number.';
        }

        if (this.PortCtrl == null) {
            hasError = true;
            errorMsg += 'Unable to load port of registration.';
        }

        if (this.FlagCtrl == null) {
            hasError = true;
            errorMsg += 'Unable to load falg.';
        }

        if (this.ClassCtrl == null) {
            hasError = true;
            errorMsg += 'Unable to load classification agency.';
        }

        if (this.DisableCtrl == null) {
            hasError = true;
            errorMsg += 'Unable to load disable.';
        }

        if (this.FileUploadCtrl == null) {
            hasError = true;
            errorMsg += 'Unable to load file uploader.';
        }

        if (this.DocsGrid == null) {
            hasError = true;
            errorMsg += 'Unable to load documents grid.';
        }

        if (this.TemplateComboCtrl == null) {
            hasError = true;
            errorMsg += 'Unable to load templates.';
        }

        if (this.CurrentSystemsTRVCtrl == null) {
            hasError = true;
            errorMsg += 'Unable to load current systems.';
        }

        if (hasError) {
            throw errorMsg;
        }

        return !hasError;
    }

    this.GetSelectedSystemID = function () {
        var selectedNodes = this.CurrentSystemsTRVCtrl.get_selectedNodes();
        if (selectedNodes.length > 0)
            return selectedNodes[0].get_value();
        else
            return null;
    }

    this.GetSelectedVesselSystemID = function () {
        var selectedNodes = this.CurrentSystemsTRVCtrl.get_selectedNodes();
        if (selectedNodes.length > 0)
            return selectedNodes[0].get_attributes().getAttribute('VsselItemCode');
        else
            return null;
    }

    this.LoadJobsByVesselItem = function (pVesselItem, pPageNo, pPageSize) {
        Landscape.MaintenanceActions.GetJobsByVesselItem(pVesselItem, pPageNo, pPageSize, function (result, e) {
            e.get_masterTableView().set_dataSource(result);
            e.get_masterTableView().dataBind();
        }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); }, this.JobsGrid);
    }

    this.LoadVesselItemDetails = function (pVesselItem) {
        this.LoadJobsByVesselItem(pVesselItem, 0, 10);
        Landscape.AdministrativeActions.GetVesselItemByCode(pVesselItem, function (result, e) {
            e.VesselItemName.innerHTML = (result.Name != null ? result.Name : '-');
            e.VesselItemDescription.innerHTML = (result.Description != null ? result.Description : '-');
            e.VesselItemSpecifications.innerHTML = (result.Specifications != null ? result.Specifications : '-');
            e.VesselItemType.innerHTML = result.VesselItemType;
            e.VesselItemIsBizCritical.innerHTML = (result.IsBusinessCritical ? 'Yes' : 'No');
            e.VesselItemIsEnvCritical.innerHTML = (result.IsEnvironmentCritical ? 'Yes' : 'No');
            e.VesselItemIsSafetyCritical.innerHTML = (result.IsSafetyCritical ? 'Yes' : 'No');
            e.VesselItemIsMandatory.innerHTML = (result.IsMandatory ? 'Yes' : 'No');
        }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); }, this);
    }
}